subroutine add_source_event(lunit)
  use event_param
  use geom_data_param,only:n_geom_obj
  use geom_data_param,only:geom_obj
  use profile_map_param,only:profile_map_srce
  use table_param,only:event_table
  use table_param,only:n_event_table
  use table_param,only:event_profile
  use table_param,only:n_event_profile
  use table_param,only:alloc_event_n_in_profile
  implicit none

  interface
     function reallocInt(p,n)
       integer,pointer,dimension(:) :: reallocInt,p
       integer,intent(in) :: n
     end function reallocInt
  end interface

  integer,intent(in) :: lunit
  
  integer :: i,j,n
  integer :: eid,pid

  do i=1,n_geom_obj
     if (.not.geom_obj(i)%does_diss) cycle
     do j=1,geom_obj(i)%diss_type

        ! Add to the event table
        n_event_table=n_event_table+1
        call alloc_event_table(n_event_table,eid)
        
        event_table(eid)%event_type=srce_event
        event_table(eid)%obj1=i
        event_table(eid)%obj_seq1=j
        event_table(eid)%removed=.false.
        event_table(eid)%is_spontaneous=.false.

        if (geom_obj(i)%diss_const_rate(j).eq.0) &
             call abnormal_stop("add_source_event",&
             "The source event cannot occur spontaneously.")

        if (profile_map_srce(i,j).eq.0) then
           ! The case when the event already exist in
           ! the profile table
           ! Add to the profile table

           n_event_profile=n_event_profile+1

           call alloc_profile_table(n_event_profile,pid)
           profile_map_srce(i,j)=pid

           event_profile(pid)%event_type=srce_event
           event_profile(pid)%rate_const=&
                geom_obj(i)%diss_const_rate(j)
           event_profile(pid)%index=i
           event_profile(pid)%sqnc=j

           ! Link the event_table and profile_table
           event_profile(pid)%n_event=1
           if (event_profile(pid)%n_event.gt.&
                event_profile(pid)%n_alloc_event) then
              event_profile(pid)%n_alloc_event=&
                   event_profile(pid)%n_alloc_event+&
                   alloc_event_n_in_profile
              event_profile(pid)%event_list=>&
                   reallocInt(event_profile(pid)%event_list,&
                   event_profile(pid)%n_alloc_event)
           end if
           event_profile(pid)%event_list(1)=eid
           event_table(eid)%profile_table_index=pid
           event_table(eid)%profile_table_seq=1
        else
           ! The case when the event doesn't exist in
           ! the profile table
           pid=profile_map_srce(i,j)

           ! Update the profile table
           n=event_profile(pid)%n_event
           n=n+1
           event_profile(pid)%n_event=n
           if (n.gt.event_profile(pid)%n_alloc_event) then
              event_profile(pid)%n_alloc_event=&
                   event_profile(pid)%n_alloc_event+&
                   alloc_event_n_in_profile
              event_profile(pid)%event_list=>&
                   reallocInt(event_profile(pid)%event_list,&
                   event_profile(pid)%n_alloc_event)
           end if
           event_profile(pid)%event_list(n)=eid
           event_table(eid)%profile_table_index=pid
           event_table(eid)%profile_table_seq=n
        end if
     end do
  end do

  return

end subroutine add_source_event
